﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;


namespace SCOSub
{
    class GetInfo_EffectAndTime 
    {
        [DllImport("kernel32.dll")]
        private  static extern int OpenProcess(int dwDesiredAccess,int bInheritHandle,int dwProcessId);
        [DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
        private static extern int CloseHandle(int hObject);
        [DllImport("kernel32.dll")]
        private static extern Int32 ReadProcessMemory(IntPtr hProcess,IntPtr lpBaseAddress,[In, Out] StringBuilder buffer,int size,out int lpNumberOfBytesRead);

        private MainForm mainform;
        public GetInfo_EffectAndTime(MainForm mainform)
        {
            this.mainform = mainform;
        }

        public int getInfo_effectandtime(int address, WorkMode workmode,out string timetip)//读内存，取时间
        {
            int PID;
            timetip = (workmode == WorkMode.Effect) ? "+00000" : "+0000000";
            try
            {
                PID = Process.GetProcessesByName("WaveEdit")[0].Id;
            }
            catch
            {
                //System.Media.SystemSounds.Hand.Play();
                
                //mainform.updataStatue(Errors.FailtoFindWEWND);
                return -1;//((workmode == WorkMode.Effect) ? "+00000" : "+0000000");
            }
            int hwnd = OpenProcess(0x1F0FFF, 0, PID);
            IntPtr baseaddress = (IntPtr)address;
            StringBuilder buffer = new StringBuilder(22);
            int num = 0;
            ReadProcessMemory((IntPtr)hwnd, baseaddress, buffer, 22, out num);
            CloseHandle(hwnd);//00:00:00:000
            Regex r = new Regex(@"([0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{3})");
            Match m = r.Match(buffer.ToString());
            if (m.Success == false)
                return -2;
            try
            {
                if (workmode == WorkMode.Effect)
                {
                    timetip = '+' + m.Groups[2].Captures[0].Value.Substring(1, 1) + m.Groups[3].Captures[0].Value + m.Groups[4].Captures[0].Value.Substring(0, 2);
                    return 0;
                }
                else
                {
                    timetip = '+' + m.Groups[1].Captures[0].Value.Substring(1, 1) + m.Groups[2].Captures[0].Value + m.Groups[3].Captures[0].Value + m.Groups[4].Captures[0].Value.Substring(0, 2);
                    return 0;
                }
            }
            catch
            {
                //System.Media.SystemSounds.Hand.Play();
                timetip = (workmode == WorkMode.Effect) ? "+00000" : "+0000000";
                //mainform.updataStatue(Errors.FailtoFindWEAddress);
                return -3;
            }
        }



    }
}